<template>
	<!-- 我的认证 -->
	<up-navbar placeholder title="我的认证" :autoBack="true" bgColor="rgba(255,255,255,0)"></up-navbar>
	<image src="@/static/images/img/sgbg.png" class="w-[100%] h-[40vh] z-[-1] absolute top-0"></image>
	<view class="px-[32rpx] pt-[32rpx]">
		<view class="flex justify-center items-center">
			<view class="flex-col flex justify-center items-center">
				<image src="@/static/images/img/rz.png" class="w-[152rpx] h-[160rpx]"></image>
				<view class="font-medium text-[32rpx] text-[#000000] leading-[48rpx] text-left font-[PingFangSC]">
					当前身份：{{ identityTypeText }}
				</view>
			</view>
		</view>
		
		<!-- 未认证状态提示 -->
		<view v-if="identityType == -1" class="mt-[16rpx]">
			<view class="w-[686rpx] h-[386rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx] flex flex-col justify-center items-center">
				<view class="text-[32rpx] text-[#333333] mb-[20rpx]">您尚未进行身份认证</view>
				<view class="text-[28rpx] text-[#999999] mb-[40rpx]">请完成身份认证以使用更多功能</view>
				<view class="w-[400rpx] h-[88rpx]">
					<up-button @click="goToCertification" text="去认证" :customStyle="{
					color: '#FFEFB8',
					fontWeight: '500',
					borderRadius: '16rpx',
					fontSize: '28rpx',
					lineHeight: '40rpx',
					background: 'linear-gradient( 98deg, #454545 0%, #000000 100%)'
				}"></up-button>
				</view>
			</view>
		</view>
		
		<!-- 身份证信息（已认证的类型都显示） -->
		<view v-if="identityType >= 0" class="mt-[16rpx]">
			<view class="w-[686rpx] h-[386rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">身份证</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="flex justify-between">
					<view class="flex-col text-center ml-[16rpx]">
						<image class="w-[280rpx] h-[158rpx]" :src="userInfo.idCardFrontUrl || '@/static/images/img/bg.png'"></image>
						<view>上传人像面</view>
					</view>
					<view class="flex-col text-center mr-[16rpx]">
						<image class="w-[280rpx] h-[158rpx]" :src="userInfo.idCardBackUrl || '@/static/images/img/bg.png'"></image>
						<view>上传国徽面</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 个人认证信息 -->
		<view v-if="identityType == 0">
			<view class="w-[686rpx] h-[286rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">芝麻信用分</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="ml-[16rpx]">
					<image class="w-[280rpx] h-[158rpx]" :src="userInfo.sesameCreditScore || '@/static/images/img/bg.png'"></image>
				</view>
			</view>
		</view>
		
		<!-- 个体认证信息 -->
		<view v-if="identityType == 1">
			<view class="w-[686rpx] h-[286rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">个体营业执照</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="ml-[16rpx]">
					<image class="w-[280rpx] h-[158rpx]" :src="userInfo.businessLicenseUrl || '@/static/images/img/bg.png'"></image>
				</view>
			</view>
			<view class="w-[686rpx] h-[286rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">芝麻信用分</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="ml-[16rpx]">
					<image class="w-[280rpx] h-[158rpx]" :src="userInfo.sesameCreditScore || '@/static/images/img/bg.png'"></image>
				</view>
			</view>
		</view>
		
		<!-- 企业认证信息 -->
		<view v-if="identityType == 2">
			<view class="w-[686rpx] h-[286rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">企业营业执照</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="ml-[16rpx]">
					<image class="w-[280rpx] h-[158rpx]" :src="userInfo.businessLicenseUrl || '@/static/images/img/bg.png'"></image>
				</view>
			</view>
			<view class="w-[686rpx] h-[286rpx] bg-[#FFFFFF] mt-[20rpx] rounded-[16rpx] p-[16rpx]">
				<view class="mb-[20rpx]">
					<view class="sh">法人信用分</view>
					<view class="w-[84rpx] h-[8rpx] bg-[#FFEFB8]"></view>
				</view>
				<view class="ml-[16rpx]">
					<image class="w-[280rpx] h-[158rpx]" :src="userInfo.sesameCreditScore || '@/static/images/img/bg.png'"></image>
				</view>
			</view>
		</view>

		<!-- 升级按钮区域 -->
		<view v-if="identityType == 0" class="w-[686rpx] h-[88rpx] mt-[214rpx]">
			<up-button @click="upgradeIdentity('individual')" text="升级个体认证" :customStyle="{
			color: '#FFEFB8',
			fontWeight: '500',
			borderRadius: '16rpx',
			fontSize: '28rpx',
			lineHeight: '40rpx',
			background: 'linear-gradient( 98deg, #454545 0%, #000000 100%)'
		}"></up-button>
		</view>
		<view v-if="identityType !== 2 && identityType >= 0" class="w-[686rpx] h-[88rpx] mt-[20rpx]">
			<up-button @click="upgradeIdentity('enterprise')" text="升级企业认证" :customStyle="{
			color: '#FFEFB8',
			fontWeight: '500',
			borderRadius: '16rpx',
			fontSize: '28rpx',
			lineHeight: '40rpx',
			background: 'linear-gradient( 98deg, #454545 0%, #000000 100%)'
		}"></up-button>
		</view>
	</view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import useUserStore from '@/store/modules/user';
import { getUserInfo } from '@/api/system/user';
import { getSettledInfo } from '@/api/buser';

const userStore = useUserStore();
const userInfo = ref({});
const identityType = ref(-1); // -1-未认证，0-个人，1-个体，2-企业

// 根据身份类型返回对应的文本
const identityTypeText = computed(() => {
	switch(identityType.value) {
		case -1: return '未认证';
		case 0: return '个人';
		case 1: return '个体';
		case 2: return '企业';
		default: return '未认证';
	}
});

// 获取用户信息
onMounted(async () => {
	try {
		if (userStore.userId) {
			// 先获取用户基本信息
			const userRes = await getUserInfo(userStore.userId);
			if (userRes.code == 200 && userRes.data) {
				userInfo.value = userRes.data;
			}
			
			// 获取商户认证信息
			const settledRes = await getSettledInfo({ userId: userStore.userId });
			if (settledRes.code == 200 && settledRes.data) {
				// 合并认证信息到用户信息中
				userInfo.value = { ...userInfo.value, ...settledRes.data };
				
				// 根据返回的用户信息判断身份类型
				if (settledRes.data.merchantType !== undefined) {
					identityType.value = settledRes.data.merchantType;
				} else if (userStore.isMerchant) {
					// 如果没有merchantType但有isMerchant标记，默认为个体商户
					identityType.value = 1;
				} else {
					// 如果没有merchantType且没有isMerchant标记，则为未认证状态
					identityType.value = -1;
				}
			} else {
				// 如果没有获取到商户认证信息，则为未认证状态
				identityType.value = -1;
			}
		}
	} catch (error) {
		console.error('获取用户信息失败', error);
		uni.showToast({
			title: '获取用户信息失败',
			icon: 'none'
		});
		// 出错时默认为未认证状态
		identityType.value = -1;
	}
});

// 去认证（未认证状态使用）
const goToCertification = () => {
	uni.navigateTo({
		url: `/pages_qc/buser/buser?merchantType=0&userId=${userStore.userId}`
	});
};

// 升级身份认证
const upgradeIdentity = (type) => {
	let merchantType = type == 'individual' ? 1 : 2;
	
	// 如果有用户信息，则传递给认证页面
	let url = `/pages_qc/buser/buser?merchantType=${merchantType}&userId=${userStore.userId}`;
	
	if (userInfo.value.shopName) {
		url += `&shopName=${encodeURIComponent(userInfo.value.shopName)}`;
	}
	
	if (userInfo.value.personName) {
		url += `&personName=${encodeURIComponent(userInfo.value.personName)}`;
	}
	
	if (userInfo.value.regionName) {
		url += `&regionName=${encodeURIComponent(userInfo.value.regionName)}`;
	}
	
	if (userInfo.value.addressDetail) {
		url += `&addressDetail=${encodeURIComponent(userInfo.value.addressDetail)}`;
	}
	
	uni.navigateTo({ url });
};
</script>

<style>
.sh {
	height: 48rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 32rpx;
	color: #000000;
	line-height: 48rpx;
	text-align: left;
	font-style: normal;
}
</style>
